package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    @Transactional
    public void add(List<OrderSetting> orderSettingList) {

        for (OrderSetting excelOrderSetting : orderSettingList) {
            //判断当前日期是否已经存在预约设置
            OrderSetting sqlOrderSetting = orderSettingDao.findByOrderDate(excelOrderSetting.getOrderDate());
            if (sqlOrderSetting != null) {
                //
                if (sqlOrderSetting.getReservations() < excelOrderSetting.getNumber()) {
                    orderSettingDao.update(excelOrderSetting);
                } else {
                    throw new BusinessRuntimeException(MessageConst.ORDER_SETTING_NUMBER_ERROR);
                }
            } else {
//                且日期是否再当前日期之后
                if (excelOrderSetting.getOrderDate().getTime() > new Date().getTime()) {
                    orderSettingDao.add(excelOrderSetting);
                } else {
                    throw new BusinessRuntimeException(MessageConst.ORDER_SETTING_DATE_ERROR);
                }

            }
        }
    }


    @Override
    public List<OrderSetting> getOrderSettingByMonth(String year, String month) {
        //SimpleDateFormat result = new SimpleDateFormat("yyyy年MM月");
        //Date date = new Date();
        month = String.format("%02d", Integer.valueOf(month));
        String string = year + "-" + month;
       /*
        try {
            date = result.parse(string);
        } catch (ParseException e) {
            throw new BusinessRuntimeException(MessageConst.QUERY_ORDER_FAIL);
        }*/
        List<OrderSetting> orderSettings = orderSettingDao.getDateyearmoth(string);
        return orderSettings;

    }

    @Override
    public void editNumByDa(Date orderDate, Integer number) {
        //根据预约设置日期查找当天预约情况
        OrderSetting dateone = orderSettingDao.findByOrderDate(orderDate);
        //根据预约设置日期查看当天已预约人数
        Integer reservatonsOne = orderSettingDao.selectReservatonsByOrderDate(orderDate);

        //如果当天没有预约设置，即数据库为空，则插入
        if (dateone == null) {
            orderSettingDao.saveOrderSetting(orderDate, number);
        } else {
            //有预约人数设置，则修改，且设置可预约人数应大于等于已预约人数，否则设置失败
            if (reservatonsOne <= number) {
                orderSettingDao.updatenumber(orderDate, number);
            } else {
                throw new BusinessRuntimeException("设置可预约人数失败");
            }
        }
    }
}
